package com.zhao.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.Result;
import com.zhao.entity.EduTeacher;
import com.zhao.entity.Vo.TeacherQuery;
import com.zhao.exceptionHandel.GuliException;
import com.zhao.service.EduTeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xiaozhao
 * @since 2021-06-04
 */
@RestController
@CrossOrigin
@RequestMapping("eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    //根据id删除讲师信息
    @DeleteMapping("{id}")
    public Result delById(@PathVariable String id){
        teacherService.removeById(id);
        return Result.ok();
    }

    //多条件组合查询讲师信息
    @PostMapping("/QueryTeacher/{currentPage}/{pageSize}")
    // @RequestBody(required = false)TeacherQuery teacherQuery 表示TeacherQuery可以为空
    public Result QueryTeacher(@PathVariable Integer currentPage, @PathVariable Integer pageSize,
                               @RequestBody(required = false)TeacherQuery teacherQuery){
        Page<EduTeacher>pageTeacher=new Page<>(currentPage,pageSize);
        QueryWrapper<EduTeacher>wrapper=new QueryWrapper<>();
        String name = teacherQuery.getName();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(level!=null){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        IPage<EduTeacher> page = teacherService.page(pageTeacher, wrapper);
        List<EduTeacher> records = page.getRecords();
        System.out.println(records);
        long total = page.getTotal();
        return Result.ok().data("total",total).data("records",records);
    }

    //添加讲师
    @PostMapping("/addTeacher")
    public Result addTeacher(@RequestBody EduTeacher teacher){
        boolean save = teacherService.save(teacher);
        return save?Result.ok():Result.error();
    }

    //通过id查询讲师信息
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return Result.ok().data("teacher",teacher);
    }

    //通过id修改讲师信息
    @PostMapping("/update")
    public Result update(@RequestBody EduTeacher teacher){
        teacherService.updateById(teacher);
        return Result.ok();
    }

    //查询所有讲师信息
    @GetMapping("/findAll")
    public Result findAll(){
        List<EduTeacher> teacherList = teacherService.list(null);
        return Result.ok().data("list",teacherList);
    }
}

